package com.alpha.eceasy.mall.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alpha.eceasy.extension.BaseServiceImpl;
import com.alpha.eceasy.mall.api.dto.MallOrderDetailBidDTO;
import com.alpha.eceasy.mall.api.entity.*;
import com.alpha.eceasy.mall.api.vo.MallOrderDetailBidVO;
import com.alpha.eceasy.mall.api.vo.MallOrderDetailVO;
import com.alpha.eceasy.mall.api.vo.MallOrderVO;
import com.alpha.eceasy.mall.mapper.MallCustomerMapper;
import com.alpha.eceasy.mall.mapper.MallOrderDetailBidMapper;
import com.alpha.eceasy.mall.mapper.MallOrderDetailMapper;
import com.alpha.eceasy.mall.mapper.MallOrderMapper;
import com.alpha.eceasy.mall.service.MallCustomerService;
import com.alpha.eceasy.mall.service.MallOrderDetailBidService;
import com.alpha.eceasy.mall.service.MallOrderDetailService;
import com.alpha.eceasy.mall.service.MallOrderService;
import com.alpha.eceasy.mall.util.StreamFilters;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 订单详情报价表（与订单详情表1对N） 服务实现类
 * </p>
 *
 * @author huangpengfei
 * @since 2024-04-24
 */
@Service
public class MallOrderDetailBidServiceImpl extends BaseServiceImpl<MallOrderDetailBidMapper, MallOrderDetailBid>
        implements MallOrderDetailBidService {
    @Resource
    private MallOrderDetailServiceImpl orderDetailService;

    public IPage<MallOrderDetailBidVO> page(Page<MallOrderDetailBidVO> page, MallOrderDetailBidDTO dto) {
        return this.selectJoinListPage(page, MallOrderDetailBidVO.class, new MPJLambdaWrapper<MallOrderDetailBid>()
                .selectAll(MallOrderDetailBid.class)
                .selectAs(MallCommoditySpecification::getSpecificationName, MallOrderDetailBidVO::getCommoditySpecificationName)
                .leftJoin(MallOrderDetail.class, MallOrderDetail::getGuid, MallOrderDetailBid::getOrderDetailGuid)
                .leftJoin(MallCommoditySpecification.class, MallCommoditySpecification::getGuid, MallOrderDetail::getCommoditySpecificationGuid)
                .eq(StrUtil.isNotBlank(dto.getSpecificationName()), MallCommoditySpecification::getSpecificationName, dto.getSpecificationName())
                .eq(StrUtil.isNotBlank(dto.getOffererGuid()), MallOrderDetailBid::getOffererGuid, dto.getOffererGuid())
                .orderByDesc(MallOrderDetailBid::getCreateTime)
        );
    }

    public IPage<MallOrderDetailVO> pageByUnicode(Page<MallOrderDetailVO> page, String offererGuid) {
        List<MallOrderDetailVO> orderDetailList = this.list(Wrappers.<MallOrderDetailBid>lambdaQuery()
                        .eq(StrUtil.isNotBlank(offererGuid), MallOrderDetailBid::getOffererGuid, offererGuid))
                .stream()
                .filter(StreamFilters.distinctByKey(MallOrderDetailBid::getUnicode))
                .map(orderDetailBid -> {
                    MallOrderDetailVO vo = orderDetailService.getVOByGuid(orderDetailBid.getOrderDetailGuid());
                    vo.setUnicode(orderDetailBid.getUnicode());
                    return vo;
                })
                .toList();
        // FIXME 虚假分页，待优化
        page.setRecords(orderDetailList.subList(
                (int) (Math.max(0, page.getCurrent() - 1) * page.getSize()),
                (int) (Math.min((page.getCurrent() + 1) * page.getSize(), orderDetailList.size()))
        ));
        return page;
    }

    public List<MallOrderDetailBidVO> listByUnicode(String unicode) {
        return this.selectJoinList(MallOrderDetailBidVO.class, new MPJLambdaWrapper<MallOrderDetailBid>()
                .selectAll(MallOrderDetailBid.class)
                .select(MallOrderDetail::getNumber)
                .select(MallCommoditySpecification::getSku)
                .selectAs(MallCommoditySpecification::getSpecificationName, MallOrderDetailBidVO::getCommoditySpecificationName)
                .leftJoin(MallOrderDetail.class, MallOrderDetail::getGuid, MallOrderDetailBid::getOrderDetailGuid)
                .leftJoin(MallCommoditySpecification.class, MallCommoditySpecification::getGuid, MallOrderDetail::getCommoditySpecificationGuid)
                .eq(StrUtil.isNotBlank(unicode), MallOrderDetailBid::getUnicode, unicode)
                .orderByDesc(MallOrderDetailBid::getCreateTime)
        );
    }

    public List<MallOrderDetailBid> getOrderDetailBidList(String offererGuid) {
        return this.list(Wrappers.<MallOrderDetailBid>lambdaQuery()
                        .eq(MallOrderDetailBid::getOffererGuid, offererGuid))
                .stream()
                .filter(StreamFilters.distinctByKey(MallOrderDetailBid::getUnicode))
                .distinct()
                .toList();
    }

    public List<String> getOrderDetailGuidList(String offererGuid) {
        return this.list(Wrappers.<MallOrderDetailBid>lambdaQuery()
                        .eq(StrUtil.isNotBlank(offererGuid), MallOrderDetailBid::getOffererGuid, offererGuid))
                .stream()
                .map(MallOrderDetailBid::getOrderDetailGuid)
                .distinct()
                .toList();
    }

    public Boolean saveDTO(MallOrderDetailBidDTO dto) {
        String nextIdStr = IdUtil.getSnowflakeNextIdStr();
        return dto.getOrderDetailBidList()
                .stream()
                .peek(orderDetailBid -> orderDetailBid.setUnicode(nextIdStr))
                .allMatch(this::save);
    }

    @Override
    public boolean updateByGuid(MallOrderDetailBid entity) {
        // 先将所有的值为未选中
        if (Objects.nonNull(entity.getState()) && entity.getState() == 1) {
            List<MallOrderDetailBid> orderDetailBidList = this.list(Wrappers.<MallOrderDetailBid>lambdaQuery()
                            .eq(MallOrderDetailBid::getOrderDetailGuid, entity.getOrderDetailGuid()))
                    .stream().peek(orderDetailBid -> orderDetailBid.setState(0)).toList();
            this.updateBatchById(orderDetailBidList);
        }
        return super.updateByGuid(entity);
    }

    public List<MallOrderDetailBid> listByOffererGuid(String offererGuid) {
        return this.list(Wrappers.<MallOrderDetailBid>lambdaQuery()
                .eq(MallOrderDetailBid::getOffererGuid, offererGuid));
    }
}
